﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Nanohope_v1._0
{
    class Map : GeneralCanvas
    {
        private KeyboardState keyState;
        private bool LEFT, RIGHT, UP, DOWN, ENTER, ESC;
        private Texture2D mapTexture, pointTexture;
        private List<MapPoint> points;
        private MapPoint pointSelected;
        private double previousTime = 0;
        private double delay = 200;

        public Map(PlayerStats stats)
        {
            pointTexture = Game1.point;

            points = new List<MapPoint>();
            addPoint();
            addPoint();
            addPoint();
            addPoint();
            addPoint();
            pointSelected = points[0];
        }

        public override void gameUpdate(GameTime gameTime)
        {
            checkKeys(gameTime);
            changeOption(gameTime.TotalGameTime.TotalMilliseconds);
        }

        private void changeOption(double time)
        {

            if (LEFT && previousTime + delay < time)
            {
                previousTime = time;
                if (pointSelected.id == 0)
                {
                    pointSelected = points[points.Count - 1];
                }
                else
                {
                    pointSelected = points[pointSelected.id - 1];
                }
            }
            if (RIGHT && previousTime + delay < time)
            {
                previousTime = time;
                if (pointSelected.id == points.Count - 1)
                {
                    pointSelected = points[0];
                }
                else
                {
                    pointSelected = points[pointSelected.id + 1];
                }
            }
        }

        public override void onDraw(SpriteBatch spriteBatch)
        {
            
            if (mapTexture != null)
            {
                spriteBatch.Draw(mapTexture, Vector2.Zero, Color.White);
            }
            for (int i = 0; i < points.Count; i++)
            {
                if (pointSelected.id != points[i].id)
                {
                    spriteBatch.Draw(pointTexture, points[i].position, Color.White);
                }
                else
                {
                    spriteBatch.Draw(pointTexture, points[i].position, Color.Red);
                }
            }
            spriteBatch.DrawString(Game1.font, "Mapa motha fucka", new Vector2(50, 50), Color.White);
        }

        protected override void checkKeys(GameTime gameTime)
        {
            keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Keys.Up))
            {
                UP = true;
            }
            else
            {
                UP = false;
            }
            if (keyState.IsKeyDown(Keys.Down))
            {
                DOWN = true;
            }
            else
            {
                DOWN = false;
            }
            if (keyState.IsKeyDown(Keys.Left))
            {
                LEFT = true;
            }
            else
            {
                LEFT = false;
            }
            if (keyState.IsKeyDown(Keys.Right))
            {
                RIGHT = true;
            }
            else
            {
                RIGHT = false;
            }
            if (keyState.IsKeyDown(Keys.Enter))
            {
                ENTER = true;
            }
            else
            {
                ENTER = false;
            }
            if (keyState.IsKeyDown(Keys.Escape))
            {
                ESC = true;
            }
            else
            {
                ESC = false;
            }
        }

        public void addPoint()
        {
            points.Add(new MapPoint(points.Count));
        }
    }
}
